/*
 * @Author: yangni
 * @Date: 2023-10-19 20:57:53
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-10 15:45:26
 * @Description: 空调详情页
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/pages/devices/device_tab/utils/device_util.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/user_entity_manager.dart';
import 'package:yt_devops/utils/enums.dart';
import '../../components/detail_grid_widget.dart';
import 'air_condition_detail_controller.dart';

class AirConditionDetailPage extends GetWidget<AirConditionDetailController> {
  final DeviceModel? deviceModel;
  const AirConditionDetailPage({Key? key, this.deviceModel}) : super(key: key);

  @override
  AirConditionDetailController get controller =>
      Get.put<AirConditionDetailController>(AirConditionDetailController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AirConditionDetailController>(
      initState: (state) {
        controller.initData(deviceModel);
      },
      dispose: (state) => Get.delete<AirConditionDetailController>(),
      builder: (_) => Scaffold(
        appBar: AppBarWidget.withRightActions(
          deviceModel?.name ?? '',
          actions: <Widget>[
            Visibility(
              visible: controller.isShowSetting,
              child: GestureDetector(
                onTap: () => Get.toNamed(
                  RouterPath.airConditionRomoteControl,
                  arguments: {
                    'deviceId': controller.deviceId,
                    'productId': controller.productId
                  },
                ),
                child: Padding(
                  padding: EdgeInsets.symmetric(
                    horizontal: 16.w,
                    vertical: 16,
                  ),
                  child: Image.asset(ImageAssets.iconSetting),
                ),
              ),
            ),
          ],
        ),
        body: StreamBuilder(
            stream: controller.dataStream,
            builder: (context, snapshot) {
              var dataSounce = snapshot.data ?? {};
              if (snapshot.data?['runningTimeFrame'] != null) {
                var map = controller
                    .runningTimeFrameToList(snapshot.data?['runningTimeFrame']);
                dataSounce.addAll(map);
              }
              return ListView.builder(
                padding: EdgeInsets.zero,
                itemBuilder: (context, index) => DeviceUtil.blockContainer(
                  Column(
                    children: [
                      DetailGridWidget(
                        title: controller.groupItem[index].title.toString(),
                        items: controller.groupItem[index].items,
                        dataSource: dataSounce,
                        isSingleRow: index == 2 ? true : false,
                        isLightTtext:
                            controller.connectStatus != ConnectStatusType.on,
                      ),
                    ],
                  ),
                ),
                itemCount: controller.groupItem.length,
              );
            }),
      ),
    );
  }
}
